package lch.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import lch.dao.PermissionDao;
import lch.dao.RoleDao;
import lch.dao.UserDao;
import lch.pojo.Permission;
import lch.pojo.Role;
import lch.pojo.User;
import lch.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @author lch
 * @date 2019-12-26 8:37
 */
@Transactional
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);//查询用户基本信息，不包含用户角色信息
        if(user==null){
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roleList = roleDao.findByUserId(userId);
        for (Role role : roleList) {
            Integer roleId = role.getId();
            Set<Permission> permissionList = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissionList);//角色关联权限
        }
        user.setRoles(roleList);//用户关联角色
        return user;
    }
}
